package com.garen.fire.dao.impl;

import com.garen.fire.dao.IFireCarGpsDao;
import com.garen.fire.entity.FireCarGps;
import com.garen.fire.entity.FireCaseInfo;
import com.garen.utils.MapBuilder;
import com.garen.common.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;


@Repository
public class FireCarGpsDaoImpl extends BaseDao<FireCarGps> implements IFireCarGpsDao {

	@PostConstruct
	private void init(){
		initOrm(FireCarGps.class);
	}
	
	//查询案情关联车的GPS信息
	@Override
	public List<FireCarGps> getFireCarGpsListByAjbh(String ajbh){
		String sql = "select DISTINCT t1.* ,(SELECT code_name from fire_code where code_type=6 and code_value=t2.car_status) as status_type, t2.car_id from fire_car_gps t1,fire_car t2,fire_case_record t3 " + 
				" where t1.device_id = t2.device_id and t2.car_id = t3.fire_car " + 
				" and t1.gps_id in(select MAX(gps_id) from fire_car_gps tt1 GROUP BY tt1.device_id) " + 
				" and t3.fire_ajbh = :ajbh ";
		Map<String,Object> paramMap = new MapBuilder()
				.put("ajbh", ajbh)
				.build();
		return getListBySQL(sql,paramMap);
	}
	//查询案情关联车的GPS信息
	@Override
	public  List<Map<String, Object>> getFireCarGpsListByZd(FireCaseInfo fireCaseInfo,List<Integer> orgIds){
		String sql = "select car_id from fire_car WHERE car_id in (select fire_car from fire_case_record where fire_ajbh in ( SELECT   t1.ajbh "
				+ " FROM fire_case_info t1,fire_org t2 "
				+ " where t1.zgzd = t2.org_uuid and t1.bjsj > :date "
				+ " and t2.org_id in (:orgIds) and t1.status <> '8' "
				+ " ORDER BY t1.bjsj desc ))" ;
		Map<String,Object> paramMap = new MapBuilder()
				 .put("date", fireCaseInfo.getBjsj())
				 .put("orgIds", orgIds)
				 .build();
		List<Map<String,Object>> list=queryForListMap(sql,paramMap);
		return list;
	}

	@Override
	public List<FireCarGps> getFireCarGpsList(Integer orgId) {
		String sql = "SELECT t2.*,t1.car_num,(SELECT code_name from fire_code  where code_type=3 and code_value=t1.car_type LIMIT 1 ) as type_name ,t1.car_capacity, (SELECT code_name from fire_code where code_type=6 and code_value=t2.status) as status_type , t1.car_alias as car_name ,t1.car_id ,t1.car_status"
				+ " from fire_car t1 ,fire_car_gps t2 "
				+ " where t1.device_id = t2.device_id and t1.car_org_id = :orgId "
				+ " order by car_id desc limit 120 ";
		
		Map<String,Object> paramMap = new MapBuilder()
				.put("orgId", orgId)
				.build();
		return getListBySQL(sql,paramMap);
	}

	@Override
	public List<FireCarGps> getFireCarGpsList(List<Integer> orgIds) {

		if(orgIds == null || orgIds.isEmpty()) return new ArrayList<>();
		String sql = "SELECT t2.*,t1.car_num,(SELECT code_name from fire_code  where code_type=3 and code_value=t1.car_type LIMIT 1 ) as type_name,t1.car_capacity,(SELECT code_name from fire_code where code_type=6 and code_value=t2.status) as status_type,t1.car_alias as car_name ,t1.car_id ,t1.car_status from fire_car t1 ,fire_car_gps t2 " +
				" ,(select MAX(gps_id) gps_id from fire_car_gps tt1 GROUP BY tt1.device_id) t3" +	
				" where t1.device_id = t2.device_id and t1.car_org_id in (:orgIds) " + 
				" and t2.gps_id = t3.gps_id ";
		Map<String,Object> paramMap = new MapBuilder()
				.put("orgIds", orgIds)
				.build();
		return getListBySQL(sql,paramMap);
	}
	@Override
	public List<FireCarGps> getFireCarGpsListbyAll(List<Integer> orgIds) {
		if(orgIds == null || orgIds.isEmpty()) return new ArrayList<>();
		String sql = "SELECT t2.*,t1.car_num,(SELECT code_name from fire_code  where code_type=3 and code_value=t1.car_type LIMIT 1 ) as type_name,t1.car_capacity," + 
				" (SELECT code_name from fire_code where code_type=6 and code_value=t2.status) as status_type,t1.car_alias as car_name ,t1.car_id ,t1.car_status " + 
				" from fire_car t1 LEFT JOIN (select g1.* from fire_car_gps g1 ,(select max(gps_id) gps_id from fire_car_gps  GROUP BY device_id) g2 where g1.gps_id = g2.gps_id) t2 " + 
				"  ON t1.device_id = t2.device_id WHERE t1.car_org_id in (:orgIds)";
		Map<String,Object> paramMap = new MapBuilder()
				.put("orgIds", orgIds)
				.build();
		return getListBySQL(sql,paramMap);
	}
}
